package com.zzyl.common;

import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.ChatCompletion;
import com.openai.models.ChatCompletionCreateParams;
import com.openai.models.ResponseFormatJsonObject;

public class AITest{
    public static void main(String[] args) {
        OpenAIClient client = OpenAIOkHttpClient.builder()
                //.apiKey(System.getenv("DASHSCOPE_API_KEY"))//读取环境变量
                .apiKey("sk-e5cc724e8acb424fad544c24a19e73e2")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .build();

        ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
                .addUserMessage("帮我写一首关于秋天的诗,输出json格式")
                .responseFormat(ResponseFormatJsonObject.builder().build())//指定返回的数据格式为json
                .model("qwen-flash-2025-07-28")
                //.model("deepseek-chat")
                .build();

        try {
            ChatCompletion chatCompletion = client.chat().completions().create(params);
            System.out.println(chatCompletion.choices().get(0).message().content().get());
        } catch (Exception e) {
            System.err.println("Error occurred: " + e.getMessage());
            e.printStackTrace();
        }
    }
}